home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1996 September / macformat-041.iso / mac / Shareware City / Graphics / MacSPD / Sources / sombrero.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-30  |  2.9 KB  |  116 lines  |  [TEXT/MMCC]

  1. /* sombrero.c - example of the use of heightfield output
  2.    From: Alexander Enzmann <70323.2461@compuserve.com>
  3.    Updated: 4/22/95 Eduard Schwan - Added code to match other
  4.    example styles, and made width/height variable based on size parameter
  5.  *    size_factor     width/height  patches
  6.  *         1            32x32
  7.  *         2            64x64
  8.  *         3           128x128
  9.  *         4           256x256
  10.    
  11. */
  12.  
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <math.h>
  16. #include "def.h"
  17. #include "drv.h"    /* display_close() */
  18. #include "lib.h"
  19.  
  20.  
  21. /* These may be read from the command line */
  22. static int size_factor      = 1;
  23. static int raytracer_format = OUTPUT_RT_DEFAULT;
  24. static int output_format    = OUTPUT_CURVES;
  25.  
  26.  
  27. /* Define constants for the sombrero function */
  28. static double a_const, b_const, c_const, two_pi_a;
  29.  
  30. static float **
  31. create_sombrero(width, height, x0, x1, z0, z1)
  32.    unsigned width, height;
  33.    double x0, x1, z0, z1;
  34. {
  35.    float **data;
  36.    double x, deltax, z, deltaz;
  37.    unsigned i, j;
  38.  
  39.    a_const = 1.0;
  40.    b_const = 1.0;
  41.    c_const = 3.0;
  42.    two_pi_a = 2.0 * 3.14159265358 * a_const;
  43.  
  44.    deltax = (x1 - x0) / (double)width;
  45.    deltaz = (z1 - z0) / (double)height;
  46.  
  47.    if ((data = malloc(height * sizeof(float *))) == NULL) {
  48.       fprintf(stderr, "HF allocation failed\n");
  49.       exit(1);
  50.       }
  51.    for (i=0,z=z0;i<height;i++,z+=deltaz) {
  52.       if ((data[i] = malloc(width * sizeof(float ))) == NULL) {
  53.      fprintf(stderr, "HF allocation failed\n");
  54.      exit(1);
  55.      }
  56.       for (j=0,x=x0;j<width;j++,x+=deltax)
  57.      data[i][j] = c_const * cos(two_pi_a * sqrt(x * x + z * z)) *
  58.               exp(-b_const * sqrt(x * x + z * z));
  59.       }
  60.    return data;
  61. }
  62.  
  63. int
  64. main(argc, argv)
  65.    int argc;
  66.    char *argv[];
  67. {
  68.    COORD4 back_color, obj_color;
  69.    COORD4 from, at, up, light;
  70.    unsigned width, height;
  71.    float **data;
  72.  
  73.     PLATFORM_INIT(SPD_SOMBRERO);
  74.  
  75.     /* Start by defining which raytracer we will be using */
  76.    if (lib_gen_get_opts(argc, argv, &size_factor, &raytracer_format,
  77.             &output_format))
  78.       return EXIT_FAIL;
  79.  
  80.    if (lib_open(raytracer_format, "sombrero.out"))
  81.       return EXIT_FAIL;
  82.  
  83.    lib_set_polygonalization(3, 3);
  84.  
  85.    /* output background color - UNC sky blue */
  86.    SET_COORD3(back_color, 0.078, 0.361, 0.753);
  87.    lib_output_background_color(back_color);
  88.  
  89.    /* output viewpoint */
  90.    SET_COORD3(from, 0, 5, -8);
  91.    SET_COORD3(at, 0, 0, 0);
  92.    SET_COORD3(up, 0, 1, 1);
  93.    lib_output_viewpoint(from, at, up, 40.0, 1.0, 0.01, 512, 512);
  94.  
  95.    /* output light sources */
  96.    SET_COORD4(light, 10, 10, -10, 1);
  97.    lib_output_light(light);
  98.  
  99.    /* Height field color - shiny_red */
  100.    SET_COORD3(obj_color, 1, 0.1, 0.1);
  101.    lib_output_color(NULL, obj_color, 0.1, 0.8, 0, 0.5, 20, 0, 1);
  102.  
  103.    /* Create the height field */
  104.    width = 32*(1 << (size_factor-1) ); /* 32, 64, 128, 256... */
  105.    height = width;
  106.    data = create_sombrero(width, height, -4.0, 4.0, -4.0, 4.0);
  107.    lib_output_height(NULL, data, width, height, -4, 4, -3, 3, -4, 4);
  108.  
  109.    lib_close();
  110.  
  111.     PLATFORM_SHUTDOWN();
  112.     return EXIT_SUCCESS;
  113. }
  114.  
  115.  
  116.